package com.hbter630.change.controller.api;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.SecureUtil;
import com.hbter630.change.entity.User;
import com.hbter630.change.service.UserService;
import com.hbter630.change.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Title: apiRegisterController
 * @Author 笑霸fianl
 * @Package com.hbter630.change.controller.api
 * @Date 2023/5/29 19:13
 * @描述: 前台注册控制器
 */
@RestController
@RequestMapping("/api")
@Api(tags = "前台注册页面")
@Slf4j
public class apiRegisterController {

    @Autowired
    private UserService userService;


    @ApiOperation(notes = "检查是否有重复的邮件",value = " 检查是否有重复的邮件",
            httpMethod = "GET", response = Result.class)
    @GetMapping("/checkemail/{email}")
    public Result checkEmail(@PathVariable("email") String email){
        return userService.checkEmail(email);
    }

    @ApiOperation(notes = "检查是否有重复的昵称",value = " 检查是否有重复的昵称",
            httpMethod = "GET", response = Result.class)
    @GetMapping("/checkusername/{username}")
    public Result checkUseName(@PathVariable("username") String username){
        return userService.checkUseName(username);
    }

    @PostMapping("/register")
    @ApiOperation(notes = "注册",value = "注册", httpMethod = "POST", response = Result.class)
    public Result register(@RequestBody  User user){
        log.info("注册用户======={}",user);
        if(BeanUtil.isNotEmpty(user)){
            String password = user.getPassword();
            String md5 = SecureUtil.md5(password);
            user.setPassword(md5);
            userService.save(user);
            return Result.ok().addData(Result.ok());
        }

        return Result.failed();
    }

}
